//
//  TTShopCartDiamondViewCell.m
//  TaoTuo
//
//  Created by admin on 2019/9/2.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTShopCartDiamondViewCell.h"
#import "TTShopCartSubDetailModel.h"
#import <UIImageView+WebCache.h>
#import "TTCommondDataModel.h"
#import "TTMessageViewController.h"
#import <SDWebImageIndicator.h>
#import <SDWebImage.h>

@interface TTShopCartDiamondViewCell ()

@property (nonatomic, strong) TTShopCartSubDetailModel *model;
@property (nonatomic, assign) BOOL hasShadow;   // 是否有阴影

@property (nonatomic, strong) UIView *bgView;       //背景
@property (nonatomic, strong) UIButton *leftBtn;
@property (nonatomic, strong) UIView *rightBgView;  // 右侧视图的背景view
@property (nonatomic, strong) UIImageView *luoshiImageView;
@property (nonatomic, strong) UILabel *luoshiLabel;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *nkLabel;
@property (nonatomic, strong) UILabel *shapeLabel;
@property (nonatomic, strong) UIButton *zsBtn;  // 证书预览按钮
@property (nonatomic, strong) UILabel *compareLabel;    // 比加入时降价还是涨价了
@property (nonatomic, strong) UILabel *priceLabel;  // 价格
///重新挑选一个按钮
@property (nonatomic, strong) UIButton *againChooseBtn;
///删除按钮
@property (nonatomic, strong) UIButton *deleteBtn;

@property (nonatomic, strong) UIImageView *selloutImageView;    // 售罄或下架图片

@property (nonatomic, strong) UIImageView *videoImageView;
@property (nonatomic, strong) UIImageView *pictureImageView;
@property (nonatomic, strong) UILabel *discountLabel;
@property (nonatomic, strong) UILabel *discountStringLabel;

@end

@implementation TTShopCartDiamondViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longGestureAction:)];
        [self.contentView addGestureRecognizer:longGesture];
        
    }
    return self;
}

- (void)longGestureAction:(UILongPressGestureRecognizer *)gesture
{
    if (gesture.state == UIGestureRecognizerStateBegan) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(longGestureActionInDiamondViewCell:deleteModel:)]) {
            [self.delegate longGestureActionInDiamondViewCell:self deleteModel:self.model];
        }
    }
}

///重新挑选
- (void)againChooseBtnClick
{
    TTMessageViewController *messageVC = [[TTMessageViewController alloc] init];
    messageVC.diamondParamsModel = self.model.DiamondsInfo;
    [[LLRouteTool getCurrentNavigationController] pushViewController:messageVC animated:YES];
}

- (void)leftBtnClick:(UIButton *)sender
{
    sender.selected = !sender.selected;
    self.model.isSelect = sender.selected;
    
    [[TTLoginSingle sharedSingle].shopCartSelectBtnSubject sendNext:@(1)];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"chooseDiamondNotify_key" object:self.model];
    
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickLeftButtonInDiamondViewCell:)]) {
        [self.delegate clickLeftButtonInDiamondViewCell:self];
    }
}

// 查看证书
- (void)lookZSBtnClick
{
    if ([self.delegate respondsToSelector:@selector(clickLookCertificateWithModel:)]) {
        [self.delegate clickLookCertificateWithModel:self.model];
    }
}

- (void)setDetailModel:(TTShopCartSubDetailModel *)detailModel hasShadow:(BOOL)hasShadow canSelect:(BOOL)canSelect cellWidth:(CGFloat)cellWidth
{
    [self setupSubviews];
    
    self.model = detailModel;
    self.hasShadow = hasShadow;
    
    self.leftBtn.selected = detailModel.isSelect;
    
    NSString *urlString = [detailModel.GoodsImage stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *imageUrl = [NSURL URLWithString:urlString];
    
//    self.luoshiImageView.sd_imageIndicator = [SDWebImageActivityIndicator grayIndicator];
    [self.luoshiImageView sd_setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"yuanluoshi"] options:SDWebImageRetryFailed];
    
//    self.luoshiLabel.text = detailModel.ProductText;
    self.luoshiLabel.text = [TTCommondDataModel toShenZhenTime][detailModel.DiamondsInfo.Location];
    self.titleLabel.text = detailModel.GoodsTitle;
    if (detailModel.GoodsDescribe.count == 1) {
        NSString *string = detailModel.GoodsDescribe.firstObject;
        string = [string stringByReplacingOccurrencesOfString:@"\"" withString:@""];
        string = [string stringByReplacingOccurrencesOfString:@"]" withString:@""];
        string = [string stringByReplacingOccurrencesOfString:@"[" withString:@""];
        self.nkLabel.text = string;
    } else {
        self.nkLabel.text = [detailModel.GoodsDescribe componentsJoinedByString:@" "];
    }
    
    NSString *value = @"";
    if (detailModel.ClarityCharacteristics.length > 1) {
        NSDictionary *dic = [TTCommondDataModel getClarityCharacteristics];
        NSArray *characteristicsArray = [detailModel.ClarityCharacteristics componentsSeparatedByString:@","];
        for (NSString *str in characteristicsArray) {
            NSString *key = [str removeFirstAndLastBlank];
            value = [NSString stringWithFormat:@"%@%@ ", value, dic[key]];
        }
    }
    self.shapeLabel.text = value;
    self.discountStringLabel.text = detailModel.DiamondsInfo.Discount;
    if ([detailModel.DiamondsInfo.Discount hasPrefix:@"-"]) {
        self.discountLabel.textColor = UIColorHex(2ECC71);
        self.discountLabel.layer.borderColor = UIColorHex(2ECC71).CGColor;
        self.discountStringLabel.textColor = UIColorHex(2ECC71);
    } else {
        self.discountLabel.textColor = UIColorHex(FF1F1F);
        self.discountLabel.layer.borderColor = UIColorHex(FF1F1F).CGColor;
        self.discountStringLabel.textColor = UIColorHex(FF1F1F);
    }
    self.discountLabel.hidden = detailModel.DiamondsInfo.Discount.length == 0;
    
    if (detailModel.SouthAfrica) {
        [self.zsBtn setTitle:@"South Africa" forState:UIControlStateNormal];
        self.zsBtn.enabled = NO;
    } else {
        [self.zsBtn setTitleColor:UIColorHex(333333) forState:UIControlStateNormal];
        if (detailModel.CertPic.length > 0) {
            NSString *zsNum = [NSString stringWithFormat:@"%@ %@", detailModel.Cert, detailModel.CertNo];
            NSString *zsStr = [NSString stringWithFormat:@"%@ 证书预览", zsNum];
            NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:zsStr];
            [attr addAttributes:@{NSUnderlineStyleAttributeName : @1, NSUnderlineColorAttributeName : kMainColor, NSForegroundColorAttributeName : kMainColor} range:[zsStr rangeOfString:@"证书预览"]];
            [attr addAttribute:NSForegroundColorAttributeName value:UIColorHex(333333) range:[zsStr rangeOfString:zsNum]];
            
            [self.zsBtn setAttributedTitle:attr forState:UIControlStateNormal];
            self.zsBtn.enabled = YES;
        } else {
            NSString *zsStr = [NSString stringWithFormat:@"%@ %@", detailModel.Cert, detailModel.CertNo];
            [self.zsBtn setTitle:zsStr forState:UIControlStateNormal];
            self.zsBtn.enabled = NO;
        }
    }
    
    if (detailModel.GoodsPriceDiff == 0) {
        self.compareLabel.hidden = YES;
        self.compareLabel.text = @"";
    } else {
        self.compareLabel.hidden = NO;
        NSString *compareStr = detailModel.GoodsPriceDiff > 0 ? @"上涨" : @"下跌";
        self.compareLabel.text = [NSString stringWithFormat:@"比加入时%@%.2f元", compareStr, ABS(detailModel.GoodsPriceDiff)];
//        self.compareLabel.hidden = detailModel.GoodsPriceDiff > 0;
    }
    
    NSString *priceStr = [NSString stringWithFormat:@"￥%@", detailModel.GoodsPrice];
    NSMutableAttributedString *priceAttrStr = [[NSMutableAttributedString alloc] initWithString:priceStr];
    [priceAttrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:11] range:[priceStr rangeOfString:@"￥"]];
    self.priceLabel.attributedText = priceAttrStr;
    
    self.bgView.frame = CGRectMake(5, 5, cellWidth - 10, detailModel.h - 10);
    if (hasShadow) {
        self.bgView.backgroundColor = [UIColor whiteColor];
        self.bgView.layer.cornerRadius = 5;
        self.bgView.layer.shadowColor = kMainColor.CGColor;
        self.bgView.layer.shadowOffset = CGSizeZero;
        self.bgView.layer.shadowOpacity = 0.2;
        self.bgView.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:self.bgView.bounds cornerRadius:5].CGPath;
    } else {
        self.bgView.backgroundColor = [UIColor clearColor];
        self.bgView.layer.cornerRadius = 0;
    }
    
    self.leftBtn.hidden = !canSelect;
    
    if (canSelect) {
        [self.rightBgView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.right.bottom.equalTo(self.bgView);
            make.left.equalTo(self.leftBtn.mas_right);
        }];
        
    } else {
        [self.rightBgView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.right.bottom.equalTo(self.bgView);
            make.left.equalTo(self.leftBtn).mas_offset(10);
        }];
    }
    
    // 售罄或下架处理
    if (detailModel.Status.intValue == 1) {
        self.selloutImageView.hidden = YES;
        self.titleLabel.textColor = [UIColor blackColor];
        self.nkLabel.textColor = UIColorHex(333333);
        self.compareLabel.textColor = UIColorHex(FF5555);
        self.priceLabel.textColor = UIColorHex(FF1F1F);
        self.leftBtn.enabled = YES;
        self.againChooseBtn.hidden = YES;
    } else {
        self.selloutImageView.hidden = NO;
        NSString *sellImageName = detailModel.Status.intValue == 2 ? @"shopcartsellout" : @"xiajia";
        self.selloutImageView.image = [UIImage imageNamed:sellImageName];
        
        self.leftBtn.enabled = NO;
        self.titleLabel.textColor = UIColorHex(999999);
        self.nkLabel.textColor = UIColorHex(999999);
        self.compareLabel.textColor = UIColorHex(FF8D8D);
        self.priceLabel.textColor = UIColorHex(FF8D8D);
        self.againChooseBtn.hidden = NO;
    }
    self.deleteBtn.hidden = self.againChooseBtn.hidden;
    if (!self.againChooseBtn.hidden) {
        self.compareLabel.hidden = YES;
    }
    
    self.pictureImageView.hidden = !detailModel.DiamondsInfo.HavePicture;
    self.videoImageView.hidden = detailModel.DiamondsInfo.Video.length == 0;
    
    
//    [self setupSubviews];
    
}

- (void)setDetailModel:(TTShopCartSubDetailModel *)detailModel hasShadow:(BOOL)hasShadow canSelect:(BOOL)canSelect
{
    [self setDetailModel:detailModel hasShadow:hasShadow canSelect:canSelect cellWidth:SCREEN_WIDTH];
}

- (void)setDetailModel:(TTShopCartSubDetailModel *)detailModel hasShadow:(BOOL)hasShadow canSelect:(BOOL)canSelect isFromCommitOrder:(BOOL)isFromCommitOrder
{
    [self setDetailModel:detailModel hasShadow:hasShadow canSelect:canSelect cellWidth:SCREEN_WIDTH];
    if (isFromCommitOrder) {
        self.againChooseBtn.hidden = YES;
        self.deleteBtn.hidden = YES;
        self.compareLabel.hidden = YES;
    }
}

- (void)deleteBtnClick
{
    if ([self.delegate respondsToSelector:@selector(clickDeleteButtonInDiamondViewCell:deleteModel:)]) {
        [self.delegate clickDeleteButtonInDiamondViewCell:self deleteModel:self.model];
    }
}

- (void)setupSubviews
{
    [self.bgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(5);
        make.right.bottom.mas_equalTo(-5);
    }];
    
    [self.leftBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(self.bgView);
        make.width.mas_equalTo(30 * kScale);
    }];
    
    [self.rightBgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.equalTo(self.bgView);
        make.left.equalTo(self.leftBtn.mas_right);
    }];
    
    [self.luoshiImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(8);
        make.left.equalTo(self.rightBgView);
        make.width.height.mas_equalTo(65 * kScale);
    }];
    
    [self.selloutImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.luoshiImageView);
    }];
    
    [self.luoshiLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.luoshiImageView.mas_bottom).mas_offset(5);
        make.centerX.equalTo(self.luoshiImageView);
    }];
    
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.luoshiImageView);
        make.left.equalTo(self.luoshiImageView.mas_right).mas_offset(10 * kScale);
        make.right.lessThanOrEqualTo(self.rightBgView).mas_offset(-10);
        make.height.mas_equalTo(20);
    }];
    
    [self.nkLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).mas_offset(3);
        make.left.equalTo(self.titleLabel);
        make.height.mas_equalTo(16);
    }];
    
    [self.pictureImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nkLabel.mas_right).mas_offset(5);
        make.centerY.equalTo(self.nkLabel);
    }];
    
    MASViewAttribute *videoLeft = self.model.DiamondsInfo.HavePicture ? self.pictureImageView.mas_right : self.nkLabel.mas_right;
    [self.videoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(videoLeft).mas_offset(5);
        make.centerY.equalTo(self.pictureImageView);
    }];
    
    [self.shapeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nkLabel.mas_bottom).mas_offset(2);
        make.left.equalTo(self.titleLabel);
        make.width.mas_lessThanOrEqualTo(200 * kScale);
        make.height.mas_greaterThanOrEqualTo(12);
    }];
    
    [self.discountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.shapeLabel.mas_right);
        make.centerY.equalTo(self.shapeLabel);
        make.width.mas_greaterThanOrEqualTo(22);
        make.height.mas_greaterThanOrEqualTo(12);
    }];
    
    [self.discountStringLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.discountLabel.mas_right).mas_offset(2);
        make.centerY.equalTo(self.shapeLabel);
    }];
    
    [self.zsBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.shapeLabel.mas_bottom);
        make.left.equalTo(self.titleLabel);
    }];
    
    [self.compareLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.zsBtn.mas_bottom).mas_offset(1);
        make.left.equalTo(self.titleLabel);
        make.bottom.equalTo(self.rightBgView).mas_offset(-3);
    }];
    
    [self.againChooseBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.zsBtn.mas_bottom).mas_offset(1);
        make.left.equalTo(self.titleLabel);
        make.bottom.equalTo(self.rightBgView).mas_offset(-3);
    }];
    [self.deleteBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.width.equalTo(self.againChooseBtn);
        make.left.equalTo(self.againChooseBtn.mas_right).mas_offset(5);
    }];
    
    [self.priceLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.rightBgView).mas_offset(-15);
        make.bottom.equalTo(self.compareLabel);
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.againChooseBtn.layer.cornerRadius = self.againChooseBtn.height * 0.5;
        self.againChooseBtn.layer.masksToBounds = YES;
        self.deleteBtn.layer.cornerRadius = self.deleteBtn.height * 0.5;
        self.deleteBtn.layer.masksToBounds = YES;
    });
    
}

- (UIView *)bgView
{
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:_bgView];
    }
    return _bgView;
}

- (UIButton *)leftBtn
{
    if (!_leftBtn) {
        _leftBtn = [UIButton createButtonWithNormalImage:[UIImage imageNamed:@"shopcart_unsel"] selectedImage:[UIImage imageNamed:@"shopcartlist_sel"] target:self action:@selector(leftBtnClick:)];
        [_leftBtn setImage:[UIImage imageNamed:@"shopcart_unable"] forState:UIControlStateDisabled];
        _leftBtn.imageView.contentMode = UIViewContentModeCenter;
        [self.bgView addSubview:_leftBtn];
    }
    return _leftBtn;
}

- (UIView *)rightBgView
{
    if (!_rightBgView) {
        _rightBgView = [[UIView alloc] init];
        [self.bgView addSubview:_rightBgView];
    }
    return _rightBgView;
}

- (UIImageView *)luoshiImageView
{
    if (!_luoshiImageView) {
        _luoshiImageView = [[UIImageView alloc] init];
        _luoshiImageView.contentMode = UIViewContentModeScaleAspectFill;
        _luoshiImageView.layer.cornerRadius = 10;
        _luoshiImageView.layer.masksToBounds = YES;
        _luoshiImageView.sd_imageIndicator = [SDWebImageActivityIndicator grayIndicator];
        [self.rightBgView addSubview:_luoshiImageView];
    }
    return _luoshiImageView;
}

- (UIImageView *)selloutImageView
{
    if (!_selloutImageView) {
        _selloutImageView = [[UIImageView alloc] init];
        _selloutImageView.contentMode = UIViewContentModeCenter;
        [self.rightBgView addSubview:_selloutImageView];
    }
    return _selloutImageView;
}

- (UILabel *)luoshiLabel
{
    if (!_luoshiLabel) {
        _luoshiLabel = [UILabel labelWithTitle:@"" color:UIColorHex(999999) fontSize:9];
        [self.rightBgView addSubview:_luoshiLabel];
    }
    return _luoshiLabel;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [UILabel labelWithTitle:@"" color:[UIColor blackColor] font:[UIFont systemFontOfSize:15]];
        _titleLabel.numberOfLines = 2;
        [self.rightBgView addSubview:_titleLabel];
    }
    return _titleLabel;
}

- (UILabel *)nkLabel
{
    if (!_nkLabel) {
        _nkLabel = [UILabel labelWithTitle:@"" color:UIColorHex(333333) fontSize:12];
        [self.rightBgView addSubview:_nkLabel];
    }
    return _nkLabel;
}

- (UIImageView *)pictureImageView
{
    if (!_pictureImageView) {
        _pictureImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"miniimage"]];
        [self.rightBgView addSubview:_pictureImageView];
    }
    return _pictureImageView;
}

- (UIImageView *)videoImageView
{
    if (!_videoImageView) {
        _videoImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"minivideo"]];
        [self.rightBgView addSubview:_videoImageView];
    }
    return _videoImageView;
}

- (UILabel *)shapeLabel
{
    if (!_shapeLabel) {
        _shapeLabel = [UILabel labelWithTitle:@"" color:UIColorHex(999999) fontSize:8];
        [self.rightBgView addSubview:_shapeLabel];
    }
    return _shapeLabel;
}

- (UILabel *)discountLabel
{
    if (!_discountLabel) {
        _discountLabel = [UILabel labelTextCenterWithTitle:@"折扣" color:UIColorHex(90F6BB) fontSize:8];
        _discountLabel.layer.cornerRadius = 3;
        _discountLabel.layer.borderWidth = 0.5;
        _discountLabel.layer.borderColor = UIColorHex(90F6BB).CGColor;
        [self.rightBgView addSubview:_discountLabel];
    }
    return _discountLabel;
}

- (UILabel *)discountStringLabel
{
    if (!_discountStringLabel) {
        _discountStringLabel = [UILabel labelWithTitle:@"" color:UIColorHex(2ECC71) fontSize:8];
        [self.rightBgView addSubview:_discountStringLabel];
    }
    return _discountStringLabel;
}

- (UIButton *)zsBtn
{
    if (!_zsBtn) {
        _zsBtn = [UIButton createButtonWithTitle:@"" titleColor:UIColorHex(333333) titleFont:[UIFont systemFontOfSize:11] backgroundColor:nil target:self action:@selector(lookZSBtnClick)];
        _zsBtn.contentEdgeInsets = UIEdgeInsetsMake(1, 0, 1, 0);
        [self.rightBgView addSubview:_zsBtn];
    }
    return _zsBtn;
}

- (UILabel *)compareLabel
{
    if (!_compareLabel) {
        _compareLabel = [UILabel labelWithTitle:@" " color:UIColorHex(FF5555) fontSize:9];
        [self.rightBgView addSubview:_compareLabel];
    }
    return _compareLabel;
}

- (UIButton *)againChooseBtn
{
    if (!_againChooseBtn) {
        _againChooseBtn = [UIButton createButtonWithTitle:@"重挑一粒" titleColor:kMainColor titleFontSize:10 borderColor:kMainColor cornerRadius:6 contentEdgeInsets:UIEdgeInsetsMake(3, 10, 3, 10) target:self action:@selector(againChooseBtnClick)];
        [self.rightBgView addSubview:_againChooseBtn];
    }
    return _againChooseBtn;
}

- (UIButton *)deleteBtn
{
    if (!_deleteBtn) {
        _deleteBtn = [UIButton createButtonWithTitle:@"删除" titleColor:UIColorHex(666666) titleFontSize:10 borderColor:UIColorHex(666666) cornerRadius:6 contentEdgeInsets:UIEdgeInsetsMake(3, 20, 3, 20) target:self action:@selector(deleteBtnClick)];
        [self.rightBgView addSubview:_deleteBtn];
    }
    return _deleteBtn;
}

- (UILabel *)priceLabel
{
    if (!_priceLabel) {
        _priceLabel = [UILabel labelWithTitle:@"" color:UIColorHex(FF1F1F) fontSize:15];
        [self.rightBgView addSubview:_priceLabel];
    }
    return _priceLabel;
}

@end
